package ph.com.gs3.formalistics.model.valueobjects.application;

import org.json.JSONException;
import org.json.JSONObject;

import ph.com.gs3.formalistics.global.constants.NotificationType;
import ph.com.gs3.formalistics.model.valueobjects.business.User;

public abstract class Notification {

	private long id;
	private NotificationType type;
	private long relatedRecordId;
	private User notifier;
	private boolean isRead;
	private String dateNotified;

	protected abstract void initializeFromJSONImpl(JSONObject json) throws JSONException;

	public abstract String getNotificationText();

	public void initializeFromJSON(JSONObject json) throws JSONException {
		initializeGenericFieldsFromJSON(json);
		initializeFromJSONImpl(json);
	}

	protected void initializeGenericFieldsFromJSON(JSONObject json) throws JSONException {

		setId(json.getLong("notification_id"));
		setRelatedRecordId(json.getLong("related_record_id"));
		setRead(1 == json.getInt("is_read"));
		setDateNotified(json.getString("date_notified"));

		// --------------------
		// Notification Type
		String rawType = json.getString("notification_type");
		setType(getNotificationTypeFromString(rawType));

		// --------------------
		// Notifier
		User notifier = new User();

		notifier.setId(json.getInt("notifier_user_id"));
		notifier.setDisplayName(json.getString("notifier_user_display_name"));
		setNotifier(notifier);

	}

	public static final NotificationType getNotificationTypeFromString(String rawType) {
		if ("NEW_REQUEST_FOR_APPROVAL".equals(rawType)) {
			return NotificationType.NEW_REQUEST_FOR_APPROVAL;
		} else if ("NEW_REQUEST_COMMENT".equals(rawType)) {
			return NotificationType.NEW_REQUEST_COMMENT;
		}

		return null;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public NotificationType getType() {
		return type;
	}

	public void setType(NotificationType type) {
		this.type = type;
	}

	public long getRelatedRecordId() {
		return relatedRecordId;
	}

	public void setRelatedRecordId(long relatedRecordId) {
		this.relatedRecordId = relatedRecordId;
	}

	public User getNotifier() {
		return notifier;
	}

	public void setNotifier(User notifier) {
		this.notifier = notifier;
	}

	public boolean isRead() {
		return isRead;
	}

	public void setRead(boolean isRead) {
		this.isRead = isRead;
	}

	public String getDateNotified() {
		return dateNotified;
	}

	public void setDateNotified(String dateNotified) {
		this.dateNotified = dateNotified;
	}

}
